<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="退料单号" prop="returnNo">
        <el-input
          v-model="queryParams.returnNo"
          placeholder="请输入退料单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产工单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入生产工单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="退料时间" prop="returnTime">
        <el-date-picker clearable
                        v-model="queryParams.returnMinTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width: 154px">
        </el-date-picker>
        至
        <el-date-picker clearable
                        v-model="queryParams.returnMaxTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width: 154px">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:ProductionReturn:add']"
        >退料</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:ProductionReturn:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:ProductionReturn:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:ProductionReturn:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProductionReturnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="退料单号" align="center" prop="returnNo" show-overflow-tooltip />
      <el-table-column label="生产工单ID" align="center" prop="orderId"  show-overflow-tooltip/>
      <el-table-column label="生产工单号" align="center" prop="orderNo"  show-overflow-tooltip/>
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="退料时间" align="center" prop="returnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span>{{ scope.row.status==1?'已退料':'已取消' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:ProductionReturn:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:ProductionReturn:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            @click="getShow(scope.row)"
            v-hasPermi="['system:ProductionReturn:query']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看按钮 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <div style="border: 1px gainsboro solid;padding: 10px 10px 10px 30px">
        <div style="display: inline-block;width: 300px;"><p>生产工单号：{{ this.productionReturnShow.orderNo }}</p></div>
        <div style="display: inline-block;width: 300px;"><p>产品名称：{{ this.productionReturnShow.productName }}</p></div>
        <div style="display: inline-block;width: 300px;"><p>退料单号：{{ this.productionReturnShow.returnNo }}</p></div>
        <br>
        <div style="display: inline-block;width: 300px;"><p>状态：{{ this.productionReturnShow.status==1?"已退料":"已取消" }}</p></div>
        <div style="display: inline-block;width: 300px;"><p>操作人：{{ this.productionReturnShow.createBy }}</p></div>
      </div>
      <div style="border: 1px gainsboro solid;">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-table :data="this.productionReturnShow.productionReturnItemList	" :row-class-name="rowTblPurchaseOrderDetailIndex" @selection-change="handleTblPurchaseOrderDetailSelectionChange" ref="tblPurchaseOrderDetail">
            <el-table-column label="仓库ID" prop="warehouseId" width="200"  />
            <el-table-column label="仓库名称" prop="warehouseName" width="200"/>
            <el-table-column label="物料ID" prop="materialId" width="200"/>
            <el-table-column label="物料编码" prop="materialCode" width="200"/>
            <el-table-column label="物料名称" prop="materialName" width="200"/>
            <el-table-column label="实际数量" prop="actualQuantity" width="200"/>
            <el-table-column label="备注" prop="remark" width="300"/>
          </el-table>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductionReturn, getProductionReturnList,getProductionReturn, delProductionReturn, addProductionReturn, updateProductionReturn } from "@/api/system/ProductionReturn";

export default {
  name: "ProductionReturn",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProductionReturnItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产退料表格数据
      ProductionReturnList: [],
      productionReturnShow:[],
      // 生产退料明细表格数据
      productionReturnItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnNo: null,
        orderNo: null,
        returnMinTime:null,
        returnMaxTime:null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        returnNo: [
          { required: true, message: "退料单号不能为空", trigger: "blur" }
        ],
        orderId: [
          { required: true, message: "生产工单ID不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "生产工单号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态(0:待退料,1:已退料,2:已取消)不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产退料列表 */
    getList() {
      this.loading = true;
      listProductionReturn(this.queryParams).then(response => {
        this.ProductionReturnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        returnNo: null,
        orderId: null,
        orderNo: null,
        returnTime: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.productionReturnItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.returnMinTime = null;
      this.queryParams.returnMaxTime = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 查询详情*/
    getShow(row) {
      this.loading = true;
      const id = row.id || this.ids
      getProductionReturn(id).then(response => {
        this.productionReturnShow = response.data;
        this.open=true;
        this.loading = false;
        this.title = "查看退料详情";
      });
    },

    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加生产退料";
      this.reset();
      this.$router.push({
        path:'/ProductionReturn/add',
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductionReturn(id).then(response => {
        this.form = response.data;
        this.productionReturnItemList = response.data.productionReturnItemList;
        this.open = true;
        this.title = "修改生产退料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.productionReturnItemList = this.productionReturnItemList;
          if (this.form.id != null) {
            updateProductionReturn(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductionReturn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除生产退料编号为"' + ids + '"的数据项？').then(function() {
        return delProductionReturn(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 生产退料明细序号 */
    rowProductionReturnItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 生产退料明细添加按钮操作 */
    handleAddProductionReturnItem() {
      let obj = {};
      obj.warehouseId = "";
      obj.materialId = "";
      obj.materialCode = "";
      obj.materialName = "";
      obj.actualQuantity = "";
      obj.remark = "";
      this.productionReturnItemList.push(obj);
    },
    /** 生产退料明细删除按钮操作 */
    handleDeleteProductionReturnItem() {
      if (this.checkedProductionReturnItem.length == 0) {
        this.$modal.msgError("请先选择要删除的生产退料明细数据");
      } else {
        const productionReturnItemList = this.productionReturnItemList;
        const checkedProductionReturnItem = this.checkedProductionReturnItem;
        this.productionReturnItemList = productionReturnItemList.filter(function(item) {
          return checkedProductionReturnItem.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleProductionReturnItemSelectionChange(selection) {
      this.checkedProductionReturnItem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ProductionReturn/export', {
        ...this.queryParams
      }, `ProductionReturn_${new Date().getTime()}.xlsx`)
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      // 重新获取数据
      vm.getList();
    });
  }
};
</script>
